package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wfyn
 * @date 2023/10/6
 */

@RestController(value = "userUserController")
@RequestMapping("/user/user")
@Api(tags = "C端-用户接口")
public class UserController {



    @Autowired
    UserService userService;
    @Autowired
    JwtProperties jwtProperties;
    @PostMapping("login")
    @ApiOperation(value = "登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {

        //微信登陆
        User user =  userService.wxLogin(userLoginDTO);
        //为用户生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        //创建UserLoginVO
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .openid(user.getOpenid())
                .token(token)
                .id(user.getId())
                .build();

        return Result.success(userLoginVO,"登录成功");
    }
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    public Result logout()
    {

        return Result.success();
    }
}
